<?php
require_once 'classes/data/Category.php';
require_once 'classes/DB.php';

/**
 * This class represents a user of the photoalbum system.
 */
class User
{	
	/**
	 * Creates an instance of the class.
	 */
	public function __construct() /*User*/
	{
	}
	
	private $userId; /*int*/

	/**
	 * Gets the user ID of the user.
	 */
	public function getUserId() /*int*/
	{
		return $this->userId;
	}

	/**
	 * Sets the user ID of the user.
	 *
	 * @param int $userId
	 * 		  The new user ID of the user.
	 */
	public function setUserId(/*int*/ $userId)
	{
		$this->userId = $userId;
	}
	
	private $userName; /*string*/
	
	/**
	 * Gets the user name of the user.
	 */
	public function getUserName() /*string*/
	{
		return $this->userName;
	}
	
	/**
	 * Sets the user name of the user.
	 *
	 * @param string $userName
	 * 		  The new user name of the user.
	 */
	public function setUserName(/*string*/ $userName)
	{
		$this->userName = $userName;
	}
	
	private $password; /*string*/
	
	/**
	 * Gets the password of the user.
	 */
	public function getPassword() /*string*/
	{
		return $this->password;
	}
	
	/**
	 * Sets the password of the user.
	 *
	 * @param string $password
	 * 		  The new password of the user.
	 */
	public function setPassword(/*string*/ $password)
	{
		$this->password = $password;
	}
	
	private $firstName; /*string*/
	
	/**
	 * Gets the first name of the user.
	 */
	public function getFirstName() /*string*/
	{
		return $this->firstName;
	}
	
	/**
	 * Sets the first name of the user.
	 *
	 * @param string $firstName
	 * 		  The new first name of the user.
	 */
	public function setFirstName(/*string*/ $firstName)
	{
		$this->firstName = $firstName;
	}
	
	private $lastName; /*string*/
	
	/**
	 * Gets the last name of the user.
	 */
	public function getLastName() /*string*/
	{
		return $this->lastName;
	}
	
	/**
	 * Sets the last name of the user.
	 *
	 * @param string $lastName
	 * 		  The new lats name of the user.
	 */
	public function setLastName(/*string*/ $lastName)
	{
		$this->lastName = $lastName;
	}
	
	private $eMail; /*string*/
	
	/**
	 * Gets the e-mail of the user.
	 */
	public function getEMail() /*string*/
	{
		return $this->eMail;
	}
	
	/**
	 * Sets the e-mail of the user.
	 *
	 * @param string $eMail
	 * 		  The new e-mail of the user.
	 */
	public function setEMail(/*string*/ $eMail)
	{
		$this->eMail = $eMail;
	}
	
	
	/**
	 * Tries to login the user and populate rest of its properties by its username and password.
	 *
	 * @return
	 * 		  True if the login is successful, false otherwise.
	 */
	public function login() /*boolean*/
	{
		/*resource*/ $result =  DB::getInstance()->executeProcedure('get_user', 
				array(DB::escapeString($this->getUserName()),
					  DB::escapeString($this->getPassword())),
				array('user_id', 'first_name', 'last_name', 'e_mail'));
		/*array*/ $userData = DB::getInstance()->getNextRow($result);
		/*boolean*/ $areCredentialsValid = ($userData['user_id'] != null);
		if ($areCredentialsValid)
		{
			$this->setUserId($userData['user_id']);
			$this->setFirstName($userData['first_name']);
			$this->setLastName($userData['last_name']);
			$this->setEMail($userData['e_mail']);
		}
		return $areCredentialsValid;
	}
	
	/**
	 * Tries to register the user using its properties and populates its ID if the register is successful.
	 * 
	 * @return
	 * 		  Error message, otherwise empty string
	 */
	public function register()
	{	
		if($this->getUserName() == "")
		{
			return "Username is empty";
		}
		if($this->getPassword() == "")
		{
			return "Password is empty";
		}
		if($this->getEMail() == "")
		{
			return "EMail is empty";
		}
		/*resource*/ $result = DB::getInstance()->executeProcedure('create_user', 
				array(DB::escapeString($this->getUserName()),
					  DB::escapeString($this->getPassword()),
					  DB::escapeString($this->getFirstName()),
					  DB::escapeString($this->getLastName()),
					  DB::escapeString($this->getEMail())),
				array('user_id'));
		/*array*/ $userData = DB::getInstance()->getNextRow($result);
		$this->setUserId($userData['user_id']);
		return $result != null ? "" : "User already exists";
	}
	
	/** 
	 * Tries to edit the user using its properties.
	 */
	public function edit()
	{
		if($this->getPassword() == "")
		{
			return "Password is empty";
		}
		if($this->getEMail() == "")
		{
			return "EMail is empty";
		}
		DB::getInstance()->executeProcedure('edit_user',
				array(DB::escapeInt($this->getUserId()),
					  DB::escapeString($this->getPassword()),
					  DB::escapeString($this->getFirstName()),
					  DB::escapeString($this->getLastName()),
					  DB::escapeString($this->getEMail())));
	}
	
	/**
	 * Tries to extract the root category of the user.
	 * 
	 * @return
	 * 		  The root category of the user or null if such doesn't exist.
	 */
	public function getRootCategory() /*Category*/
	{
		/*resource*/ $result = DB::getInstance()->executeProcedure('get_root_categories',
				array(DB::escapeInt($this->getUserId())));
		/*array*/ $categoryData = DB::getInstance()->getNextRow($result);
		/*Category*/ $rootCategory = new Category(DB::getInstance());
		$rootCategory->setCategoryId($categoryData['category_id']);
		$rootCategory->setName($categoryData['name']);
		$rootCategory->setUserId($this->getUserId());
		$rootCategory->setParentId($categoryData['parent_id']);
		return $rootCategory;
	}
} // end class
?>